package com.yang.controller.admin;

import com.yang.bean.*;
import com.yang.bean.common.BasePageInfo;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.common.CommonData;
import com.yang.bean.fxy.bo.IdsBo;
import com.yang.bean.yvo.PasswordBO;
import com.yang.service.admin.AdminUserService;
import com.yang.utils.UserUtil;
import jdk.nashorn.internal.ir.ReturnNode;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @author TrueEnd
 * @date 2022/09/07
 */
@RestController
@RequestMapping("admin")
public class AdminUserController {

    @Autowired
    AdminUserService adminUserService;

    @RequestMapping("profile/lsnotice")
    public BaseRespVo profileLsnotice(String title,String type,BasePageInfo info){
        CommonData<MarketNoticeAdmin> data = adminUserService.profileLsnotice(title, type, info);
        return BaseRespVo.ok(data);
    }
    @RequestMapping("profile/catnotice")
    public BaseRespVo profileCatnotice(@RequestBody  MarketNoticeAdmin noticeAdmin){
        adminUserService.profileCatnotice(noticeAdmin);
        return BaseRespVo.ok();
    }

    @RequestMapping("profile/brmnotice")
    public BaseRespVo profileBrmnotice(@RequestBody IdsBo ids){
        adminUserService.profileBrmnotice(ids);
        return BaseRespVo.ok();
    }
    //TODO handler方法和权限建立耦合关系，访问这个Handler反方法需要注解的权限
//    @RequiresPermissions("aaa")
    @RequestMapping("user/list")
    public BaseRespVo userList(String username, String mobile, BasePageInfo info){
        CommonData<MarketUser> data = adminUserService.userList(username, mobile, info);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("user/detail")
    public BaseRespVo userDetail(Integer id){
        MarketUser data = adminUserService.userDetail(id);
        return BaseRespVo.ok(data);
    }


    @RequestMapping("address/list")
    public BaseRespVo addressList(String name, Integer userId, BasePageInfo info){
        CommonData<MarketAddress> data = adminUserService.addressList(name, userId, info);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("collect/list")
    public BaseRespVo collectList(Integer userId, Integer valueId, BasePageInfo info){
        CommonData<MarketCollect> data = adminUserService.collectList(userId, valueId, info);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("footprint/list")
    public BaseRespVo footprintList(Integer userId, Integer goodsId, BasePageInfo info){
        CommonData<MarketFootPrint> data = adminUserService.FootprintList(userId, goodsId, info);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("history/list")
    public BaseRespVo historyList(Integer userId, String keyword, BasePageInfo info){
        CommonData<MarketSearchHistory> data = adminUserService.HistoryList(userId, keyword, info);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("feedback/list")
    public BaseRespVo feedbackList(String username, Integer id, BasePageInfo info){
        CommonData<MarketFeedback> data = adminUserService.FeedbackList(username, id, info);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("region/list")
    public BaseRespVo regionList(){
        CommonData<MarketRegion> data=adminUserService.regionList();
        return BaseRespVo.ok(data);
    }


    @RequestMapping("brand/list")
    public BaseRespVo brandList(Integer id,String name,BasePageInfo info) {
        CommonData<MarketBrand> data= adminUserService.brandList(id,name,info);
        return BaseRespVo.ok(data);

    }

    @RequestMapping("brand/delete")
    public BaseRespVo brandDelete(@RequestBody MarketBrand brand){
        adminUserService.brandDelete(brand);
        return BaseRespVo.ok(1);
    }
    @RequestMapping("brand/update")
    public BaseRespVo brandUpdate(@RequestBody MarketBrand brand){
        MarketBrand data=adminUserService.brandUpdate(brand);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("brand/create")
    public BaseRespVo brandCreate(@RequestBody MarketBrand brand){
        MarketBrand data=adminUserService.brandCreate(brand);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("user/update")
    public BaseRespVo userUpdate(@RequestBody MarketUser user){

        adminUserService.userUpdate(user);
        return BaseRespVo.ok(1);
    }

    @RequestMapping("profile/bcatnotice")
    public BaseRespVo profileBcatnotice(@RequestBody IdsBo ids){
        adminUserService.profileBcatnotice(ids);
        return BaseRespVo.ok();
    }

    @Autowired
    DefaultWebSessionManager defaultWebSessionManager;
    @RequestMapping("profile/password")
    public BaseRespVo password(@RequestBody PasswordBO passwordBO, HttpServletRequest request) {
        //{"oldPassword":"123451","newPassword":"123456","newPassword2":"123456"}
        //TODO 获取用户的信息
        MarketAdmin adminMsg =  UserUtil.getAdminMsg(request,defaultWebSessionManager);
        if ("admin123".equals(adminMsg.getUsername())) {
            return BaseRespVo.error("超级管理员不能修改");
        }
        if (!adminMsg.getPassword().equals(passwordBO.getOldPassword())) {
            return BaseRespVo.error("原始的密码不对");
        }
        adminUserService.updatePassword(passwordBO,adminMsg);

        return BaseRespVo.ok();
    }





}
